package Testclass.vueTest;

import Dao.jdbcTools;
import Model.VueStu;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/vueStuad")
public class VueTestadd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json");
        String data1 = req.getParameter("data1");
        System.out.println("data1 = " + data1);
        JSONObject jsonObject = JSONObject.parseObject(data1.toString());
       // Integer id = Integer.valueOf( jsonObject.getString("id"));
        String name = jsonObject.getString("name");
        System.out.println("name = " + name);
        Integer age = Integer.valueOf( jsonObject.getString("age"));
        String hobit = jsonObject.getString("hobit");
       VueStu vueStu = new VueStu();
       // vueStu.setId(id);
        vueStu.setName(name);
        vueStu.setAge(age);
        vueStu.setHobit(hobit);
        int add = 0;
        try {
            add = jdbcTools.add("insert into vuestu value(null,?,?,?)",name,age,hobit);
            String res = JSON.toJSONString(add);
            System.out.println("后端json格式的字符串 = " + res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = resp.getWriter();
        if (add>0){
            System.out.println(add);

            writer.write("success");
        }else {
            writer.write("error");
        }
        writer.flush();
        writer.close();
    }
}
